<style>
    #paypal-button-container {
        margin: auto;
        padding: 20px 0px;
        width: 350px !important;
        text-align: center
    }

    #paypal-button-container .xcomponent-outlet {
        z-index: 1;
    }

    #pp-message {
        width: 350px !important;
        margin: auto;
    }
</style>
<style>
    .product_card-price,.product-price{
        display: none;
    }
    .block_slides .slide-item-content-center .slide-item-title::before{
        display: none !important;
    }
</style>
<div id="pp-message" data-pp-message data-pp-amount="7.28"></div>
<div id="paypal-button-container"></div>
<script>
    (function () {
        var paypalURL = "https://www.paypal.com/sdk/js?client-id=sb&buyer-country=US&components=buttons,funding-eligibility,marks&commit=true&currency=GBP";
        var paypal_redirect_url = '';

        function createPayment(resolve) {
            fetch('/checkouts/payment/paypalcreditcard/ECOrderPay?is_ajax=1&order_number=TD00389', {
                method: 'POST'
            }).then(function (res) {
                return res.json();
            }).then(function (data) {
                if (data.status) {
                    paypal_redirect_url = data.data.return_url;
                    resolve(data.data.paypal_order_id)
                }
                else {
                    if (data.data.L_ERRORCODE0 == 10736) {
                        location.href = '/26817-3bba66/checkouts/3bba66cea15645c60511e415cc192a23?previous_step=contact_information&step=shipping_method&ref=%2F26817-3bba66%2Fcheckouts%2F3bba66cea15645c60511e415cc192a23%3Fprevious_step%3Dpayment_method%26step%3Dpayment_gateway' + '&payment_failed_msg=' + data.data.L_LONGMESSAGE0;
                    }
                    alert(data.data.L_LONGMESSAGE0);
                    resolve(false)
                }
            });
        }
        function paypalInit() {
            var FUNDING_SOURCES = [paypal.FUNDING.CARD];
            FUNDING_SOURCES.forEach(function (fundingSource) {
                var button = paypal.Buttons({
                    style: {
                        layout: 'horizontal',  // horizontal | vertical
                    },
                    fundingSource: fundingSource,
                    createOrder: function (data, actions) {
                        return new Promise(function (resolve) {
                            const paypalVerified = moi.onEvent("paypalVerified");
                            if (paypalVerified.results && paypalVerified.results.length) {
                                paypalVerified.results[0].then(function (result) {
                                    if (result) {
                                        createPayment(resolve)
                                    } else {
                                        resolve(false)
                                    }
                                })
                            } else {
                                createPayment(resolve)
                            }
                        })
                    },
                    onApprove: function (data, actions) {
                        if (paypal_redirect_url != '') {
                            //location.href = paypal_redirect_url;
                            //return;
                            //alert(paypal_redirect_url);
                            //console.log(data);
                        }
                        //return actions.redirect();
                        return fetch(paypal_redirect_url, {
                            method: 'POST'
                        }).then(function (res) {
                            return res.json();
                        }).then(function (res) {
                            if (res.code > 0) {
                                alert(res.msg);
                            }
                            else {
                                location.href = res.data.success_url;
                            }
                        });
                    }
                });
                if (button.isEligible()) {
                    button.render('#paypal-button-container')
                }
            })
        }

        moi.createScript(paypalURL, function () {
            if (window.paypal) {
                paypalInit();
            } else {
                const timer = setInterval(() => {
                    if (window.paypal) {
                        paypalInit();
                        clearInterval(timer);
                    }
                }, 200);
            }
        });
    })();

</script>